<%@ page language="java" pageEncoding="UTF-8"%>
<link href="${baseURL}/themes/plugin/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="${baseURL}/themes/plugin/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="${baseURL}/themes/plugin/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js" charset="UTF-8"></script>
<script type="text/javascript" src="${baseURL}/js/module/bootstrap/common_edit.js"></script>
<!-- 明细与列表在同一个页面 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editCheckLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<p class="modal-title" id="editCheckLabel">${module.moduleName}编辑</p>
			</div>
			<div class="modal-body cont-table">
				<div class="table-normal">
					<form method="post" id="formdetail"></form>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-blue" onclick="FF_Save()"> 保存 </button>
				<button type="button" class="btn btn-gray" data-dismiss="modal"> 关闭 </button>
			</div>
		</div>
	</div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script type="text/javascript">
	//编辑，新增弹出框
	var detailWin = null, curentData = null;
	var formdetail = $("#formdetail");
	var fieldsData = '${module.moduleFields}';
	var columnCount = 1;
	var dicObj = '${module.dic}';

    // common_edit.js 需要用到这两个参数，需要在这里定义一下
    var mdId = '${param.mdId}'
    var mdCode = '${param.mdId}'

	if(dicObj){
		dicObj = JSON.parse(dicObj);
	}
	var editor = null;
	var eventObj;
	<c:if test="${module.editedJs!=null}">
	//加载编辑的事件js，因此要放在eventObj变量之后
	if('${module.editedJs}'!=''){
		loadScript('${baseURL}/${module.editedJs}');
	}
	</c:if>
	
	function FF_ShowDialog(data, isAddNew) {
		curentData = data;
		if(editor==null){
			editor = initForm(fieldsData, formdetail, dicObj, null, columnCount, eventObj);
		}
		var fields = editor.fields;
		if (curentData) {
			for ( var key in fields) {
				var name = fields[key].code;
				var value = curentData[fields[key].code];
				if(editor.element[name]){
					if((editor.element[name].type == "comboBox" || editor.element[name].type == "checktree" || editor.element[name].type == "radio") && fields[key].options.valueFieldID){
						var valueFieldId = fields[key].options.valueFieldID.replace(editor.options.prefix,"");
						if(curentData[valueFieldId]){
							if(curentData[valueFieldId]){
								value = curentData[valueFieldId];
							}
						}
					}
					if(value!=null && value!=""){
						if(editor.element[name].type == "radio"){
							editor.element[name].element.clear();
						}
						editor.element[name].element._setValue(value);
					}else{
						if(editor.element[name].type != "radio"){
							editor.element[name].element.clear();
						}
					}
				}else{
					if(fields[key].type == "textarea"){
						$("textarea[name='"+ editor.options.prefix + name + "']").not(":checkbox").not(":radio").val(value);
					}else{
						$("input[name='"+ editor.options.prefix + name + "']").not(":checkbox").not(":radio").val(value);
					}
				}
			}
		} else {
			for ( var key in fields) {
				var name = fields[key].code;
				if(editor.element[name]){
					if(editor.element[name].type != "radio"){
						editor.element[name].element.clear();
					}
				}else{
					if(fields[key].type == "textarea"){
						$("textarea[name='" + editor.options.prefix + name + "']").not(":checkbox").not(":radio").val("");
					}else{
						$("input[name='" + editor.options.prefix + name + "']").not(":checkbox").not(":radio").val("");
					}
				}
			}
		}
		
		if(eventObj && eventObj.initForm){
			eventObj.initForm(data);
		}
	}

	function FF_Save() {
		if(eventObj && eventObj.beforeSave){
			eventObj.beforeSave();
		}
		formdetail.attr("action",
				"${baseURL}/jdbc/common/basecommonsave/save.do?mdId=${param.mdId}&strategy=${strategy}");
		BT.submitForm(formdetail, function(data) {
			if (data && data.hasOk) {
				$('#editModal').modal('hide');
				search();
				BT.showSuccess('操作成功!');
			} else {
				BT.showError("操作失败!失败信息如下:\n" + data.message + "\n如您对以上信息有疑问，请联系管理人员!");
			}
		});
	}

	// 动态加载js脚本文件
	function loadScript(url) {
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = url;
		document.body.appendChild(script);
	}
</script>
